package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.Ter3gExpenses;
import beans.TerElecExpenses;

import utils.HibernateUtil;





public class TerminalExpensesDao {
	private SessionFactory sessionFactory;

	public TerminalExpensesDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;

	/**
	 * 查电费
	 * 
	 * @param terminalId
	 * @return
	 */
	public List<TerElecExpenses> terElecExpensesList(int terminalId) {

		TerminalDao terminalDao = new TerminalDao();

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(TerElecExpenses.class);
		c.add(Restrictions.eq("terminal",
				terminalDao.getTerminalById(terminalId)));
		c.addOrder(Order.desc("updateTime"));
		List<TerElecExpenses> terminalExpenses = c.list();
		tr.commit();
		session.close();
		return terminalExpenses;
	}

	/**
	 * 查流量
	 * 
	 * @param terminalId
	 * @return
	 */
	public List<Ter3gExpenses> ter3gExpensesList(int terminalId) {

		TerminalDao terminalDao = new TerminalDao();

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Ter3gExpenses.class);
		c.add(Restrictions.eq("terminal",
				terminalDao.getTerminalById(terminalId)));
		c.addOrder(Order.desc("updateTime"));
		List<Ter3gExpenses> terminalExpenses = c.list();
		tr.commit();
		session.close();
		return terminalExpenses;
	}
}
